package com.achang.achangmall.search.controller;

import com.achang.achangmall.search.service.ProductSaveService;
import com.achang.common.exception.BizCodeEnum;
import com.achang.common.to.es.SkuEsModel;
import com.achang.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/******
 @author 阿昌
 @create 2021-10-03 20:18
 *******
 */

@RequestMapping("/search")
@RestController
@Slf4j
public class ESSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    //上架商品
    @PostMapping("/save/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> esModels) {
        Boolean flag = false;
        try {
            flag =  productSaveService.productStatusUp(esModels);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("ESSaveController,商品上架错误：{}",e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }

        if (!flag){
            return R.ok();
        }else {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
    }

}
